<?php
class ControllerCommonSeoUrl extends Controller {
	public function index() {
		// Add rewrite to url class
		if ($this->config->get('config_seo_url')) {
			$this->url->addRewrite($this);
		}
			
		// Decode URL
		if (isset($this->request->get['_route_'])) {
			$parts = explode('/', $this->request->get['_route_']);
			
			$pl = count($parts) - 1;
			$params = substr($parts[$pl], 0, -5);
			$route = substr($params, 0, 1);
			$id = substr($params, 1);
			
			$pl_2 = stripos($params, "_");
			if($pl_2){
				$special_route = substr($params, 0, $pl_2);
				$category_id = substr($params, $pl_2 + 1);
			}else{
				$special_route = '';
				$category_id = false;
			}
			$daily = $this->word2url2($this->language->get('url_daily/daily'));
			$new = $this->word2url2($this->language->get('url_new/new'));
			$bestsell = $this->word2url2($this->language->get('url_bestsell/bestsell'));

			if($daily == $params || $daily == $special_route) {
				$this->request->get['route'] = 'daily/daily';
				if($category_id){
					$this->request->get['category_id'] = $category_id;
				}
			}else if($new == $params || $new == $special_route) {
				$this->request->get['route'] = 'new/new';
				if($category_id){
					$this->request->get['category_id'] = $category_id;
				}
			}else if($bestsell == $params || $bestsell == $special_route) {
				$this->request->get['route'] = 'bestsell/bestsell';
				if($category_id){
					$this->request->get['category_id'] = $category_id;
				}
			}else if("c" == $route){
				$this->request->get['route'] = 'product/category';
				$this->request->get['category_id'] = $id;
			}else if("p" == $route){
				$this->request->get['route'] = 'product/product';
				$pl_3 = stripos($id, "_");
				if($pl_3){
					$this->request->get['product_id'] = substr($id, 0, $pl_3);
					$this->request->get['category_id'] = substr($id, $pl_3 + 1);
				}else{
					$this->request->get['product_id'] = $id;
				}
			}else if("s" == $route){
				$this->request->get['route'] = 'search/search';
				if($id != "0"){
					$this->request->get['category_id'] = $id;
				}
				if($pl > 0 && !isset($this->request->get['keyword'])){
					$pl = $pl - 1;
					$this->request->get['keyword'] = $this->url2word($parts[$pl]);
				}
			}else if("i" == $route){
				$this->request->get['route'] = 'information/information';
				$this->request->get['information_id'] = $id;
			}else if("b" == $route){
				$this->request->get['route'] = 'product/manufacturer';
				$this->request->get['manufacturer_id'] = $id;
			}else if("f" == $route){
				$this->request->get['route'] = 'promotion/promotion';
				$this->request->get['promotion_id'] = $id;
				if($pl > 0 && !isset($this->request->get['promotion_name'])){
					$pl = $pl - 1;
					$this->request->get['promotion_name'] = $this->url2word($parts[$pl]);
				}
			}

			if (isset($this->request->get['route'])) {
				return $this->forward($this->request->get['route']);
			}
		}
	}
	
	public function rewrite($url_f, $route, $args='', $name) {
		
		if ($this->config->get('config_seo_url')) {
			
			$categoryId = '';
			$productId = '';
			$infoId = '';
			$brandId = '';
			$promotionId = '';
			$query = '';
			
			$data = array();
			parse_str($args, $data);
			foreach ($data as $key => $value) {
		
				if($key == 'category_id'){
					$categoryId = $value;
				}else if($key == 'product_id'){
					$productId = $value;
				}else if($key == 'information_id'){
					$infoId = $value;
				}else if($key == 'manufacturer_id'){
					$brandId = $value;
				}else if($key == 'promotion_id'){
					$promotionId = $value;
				}else {
					$query .= '&' . $key . '=' . $value;
				}
			}
			
			if(strlen($query) > 0){
				$query = '?' . ltrim($query, '&'); 
			}
			
			$url = $this->word2url($name);
			if($route == 'product/category'){
				$url = $url_f . $url . '/c' . $categoryId . '.html' . $query;
			}else if($route == 'product/product'){
				$url = $url_f . $url . '/p' . $productId;
				if($categoryId){
					$url .= '_' . $categoryId;
				}
				$url .= '.html' . $query;
			}else if($route == 'search/search'){
				if($categoryId){
					$url = $url_f . $url . '/s' . $categoryId . '.html' . $query;
				}else{
					$url = $url_f . $url . '/s0' . '.html' . $query;
				}
			}else if($route == 'information/information'){
				$url = $url_f . $url . '/i' . $infoId . '.html' . $query;
			}else if($route == 'product/manufacturer'){
				$url = $url_f . $url . '/b' . $brandId . '.html' . $query;
			}else if($route == 'promotion/promotion'){
				$url = $url_f . $url . '/f' . $promotionId . '.html' . $query;
			}else if($route == 'new/new' || $route == 'daily/daily' || $route == 'bestsell/bestsell'){
				$url = $url_f . $this->word2url($this->language->get('url_' . $route));;
				if($categoryId){
					$url .= '_' . $categoryId;
				}
				$url .= '.html' . $query;
			}else{
				$url = $url_f . $url . '.html' . $query;
			}
			
			return $url;
		}
		if(strlen($args) > 0){
			return $url_f . 'index.php?route=' . $route . '&' . $args;
		}
		return $url_f . 'index.php?route=' . $route;
	}
	
	public function word2url($str = ''){
		if($str){
			$str = trim($str);
			$str = preg_replace("/\s+/", "-", $str);
			$str = preg_replace("/_+/", "-", $str);
			$str = '/' . $str;
		}
		return $str;
	}
	
	public function url2word($str = ''){
		if($str){
			$str = trim($str);
			$str = preg_replace("/-+/", " ", $str);
		}
		return $str;
	}
	
	public function word2url2($str = ''){
		if($str){
			$str = trim($str);
			$str = preg_replace("/\s+/", "-", $str);
			$str = preg_replace("/_+/", "-", $str);
		}
		return $str;
	}
}
?>